package com.ymatou.iapi.trading.operate.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.OrderItemVo;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.req.OnXloboBillDeletedReq;
import com.ymatou.iapi.trading.operate.parameter.req.OrderLogisticsInfoPo;
import com.ymatou.iapi.trading.operate.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.trading.operate.service.LogisticsDeliveryService;
import com.ymatou.iapi.trading.operate.service.OnXloboBillDeletedCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.TestDataManager;
/**
*响应贝海面单删除的请求。MQ回调
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_OnXloboBillDeleted {
	private static OnXloboBillDeletedReq onXloboBillDeletedReq;
	private static OnXloboBillDeletedCall onXloboBillDeletedCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("响应贝海面单删除的请求。MQ回调");
	}

	@Before
	public void caseUp() {
		onXloboBillDeletedReq = new OnXloboBillDeletedReq();
		onXloboBillDeletedCall = new OnXloboBillDeletedCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(onXloboBillDeletedCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 订单为电商件,物流为贝海直邮
	 * @return PlaceOrderResp
	 */
	public PlaceOrderResp placeOrder(String receiveName) {

		PlaceOrderReq placeorderBean = PlaceOrderService.getDefaultOrderReq();

		OrderItemVo OrderItemVo = placeorderBean.getOrderGroups().get(0)
				.getOrders().get(0).getOrderItems().get(0);

		placeorderBean.getOrderGroups().get(0).getOrders().get(0)
				.setEcommerceFlag(true);

		placeorderBean.getOrderGroups().get(0).getOrders().get(0)
				.setReceiveName(receiveName);

		OrderItemVo.setLogisticsType(LogisticsTypeEnum.SailProtect);

		return PlaceOrderService.callPlaceOrderService(placeorderBean);

	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_OnXloboBillDeleted_001() {
		Logger.start(true, "同一订单下所有面单全部删除才能回滚订单");
		try {

			OrderCallService orderCallService = new OrderCallService();

			PlaceOrderResp placeOrderResp = PlaceOrderService
					.placeOrderWithOutCoupon();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();
			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);

			String billNo = String.format("YD%s",
					TestDataManager.getRandomNum(10));

			// 码头保存物流单号
			PlaceOrderService.saveDeliverSummary(orderId, sellerId, billNo);

			// 商家发货
			orderCallService.dispatchedByXlobo(orderId, sellerId);

			onXloboBillDeletedReq.setBillNo(billNo);

			onXloboBillDeletedCall.setData(onXloboBillDeletedReq);

			onXloboBillDeletedCall.callService();

			Logger.verifyEquals("ok", onXloboBillDeletedCall.getReturnData(),
					"验证Code");

			DefaultWapper database = new DefaultWapper();
			// 订单主表
			List<Map> ymtOrders = database.selectOrderByOderId(orderId);
			List<Map> list = new DefaultWapper()
					.selectOrderFrozenDetailByOrderId(orderId);
			Logger.verifyEquals(0, list.size(),
					"验证Ymt_OrderFrozenDetail 表数据已删除");
			Logger.verifyEquals(17,
					(Integer) ymtOrders.get(0).get("iTradingStatus"),
					"验证iTradingStatus");

			OrderLogisticsInfoPo orderLogisticsInfoPo = LogisticsDeliveryService
					.getLogisticsDeliveryByOrderId(orderId).get(0);

			Logger.verifyEquals(true, orderLogisticsInfoPo.getDeleted(),
					"验证Ymt_OrderLogisticsInfo 表 deleted");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_OnXloboBillDeleted_002() {
		Logger.start(true, "同一订单下有面单未全部删除");
		try {

			OrderCallService orderCallService = new OrderCallService();

			PlaceOrderResp placeOrderResp = PlaceOrderService
					.placeOrderWithOutCoupon();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();
			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);

			String billNo1 = String.format("YD%s",
					TestDataManager.getRandomNum(10));

			String billNo2 = String.format("YD%s",
					TestDataManager.getRandomNum(10));
			// 码头保存物流单号
			PlaceOrderService.saveDeliverSummary(orderId, sellerId, billNo1);
			PlaceOrderService.saveDeliverSummary(orderId, sellerId, billNo2);

			// 商家发货
			orderCallService.dispatchedByXlobo(orderId, sellerId);

			onXloboBillDeletedReq.setBillNo(billNo1);

			onXloboBillDeletedCall.setData(onXloboBillDeletedReq);

			onXloboBillDeletedCall.callService();

			Logger.verifyEquals("ok", onXloboBillDeletedCall.getReturnData(),
					"验证Code");

			DefaultWapper database = new DefaultWapper();
			// 订单主表
			List<Map> ymtOrders = database.selectOrderByOderId(orderId);

			Logger.verifyEquals(3,
					(Integer) ymtOrders.get(0).get("iTradingStatus"),
					"验证iTradingStatus");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}